package com.example.demo.controller;


import com.example.demo.entity.User;
import com.example.demo.service.IUserService;
import org.apache.catalina.Session;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2018-12-21
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;
    @RequestMapping("/")
    public String get(){
        List<User> list=userService.list();
        List<Map<String,String>> maps=userService.findCityMsg();
        return list.get(0).getUsername();
    }
    /**
     * 登录
     * @param model
     * @param response
     * @return
     */
    @RequestMapping("/login")
    public String login(String username, String password, Model model, HttpServletRequest request) {
        ModelAndView mv=new ModelAndView();

        HttpSession session= request.getSession();

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
        try {
            subject.login(usernamePasswordToken);
            return "redirect:index";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg","用户名不存在!");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误!");
            return "login";
        }
    }
    /**
     * 登录页面
     * @param model
     * @param response
     * @return
     */
    @RequestMapping("/tologin")
    public String tologin(Model model, HttpServletResponse response) {
        model.addAttribute("name", "simonsfan");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("login");
        return "/login";
    }
    /**
     * 主页面
     * @param model
     * @param response
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(Model model, HttpServletResponse response) {
        model.addAttribute("name", "simonsfan");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
    /**
     * 跳转页面
     * @param model
     * @param response
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add(Model model, HttpServletResponse response) {
        model.addAttribute("name", "simonsfan");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("add");
        return mv;
    }
    /**
     * 未授权页面
     * @param model
     * @param response
     * @return
     */
    @RequestMapping("/noAuth")
    public ModelAndView onAuth(Model model, HttpServletResponse response) {
        model.addAttribute("name", "simonsfan");
        ModelAndView mv=new ModelAndView();
        mv.setViewName("noAuth");
        return mv;
    }
}
